﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using config.model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using config.service;
using System.Windows.Documents;
using System.Data;
namespace config.viewmodel
{
    partial class HomeViewModel:ObservableObject
    {

        [ObservableProperty]
        private DeviceInfo _deviceInfo = new DeviceInfo() { };
        private readonly ISerial _serial;
        private readonly IFFBProtocol _dataParser;
        [ObservableProperty]
        private int _maxStreeingAngle = 900;
        [ObservableProperty]
        private int _streeingAngle = 45;
        [ObservableProperty]
        private ObservableCollection<string> _portList;
        [ObservableProperty]
        private bool _isConnected = false;
        [ObservableProperty]
        private string _selectProt;
        public RelayCommand RefreshPort { get;  }
        public RelayCommand Connect { get; }
        public RelayCommand DisConnect { get; }
        public RelayCommand<string?> SetAngle { get; } 
        public RelayCommand SetCenterPoint { get; }
        //TODO:后期实现依赖注入
        public HomeViewModel(ISerial serial, IFFBProtocol ffbProtocol) 
        {
            _serial = serial;
            _dataParser = ffbProtocol;
            RefreshPort = new RelayCommand(OnRefreshPort);
            Connect = new RelayCommand(()=>
            {
                if(string.IsNullOrEmpty(SelectProt))
                {
                    MessageBox.Show("请选择串口");
                    return;
                }
                if(_serial.Connect(SelectProt))
                {
                    Byte[] getDeviceInfo = FFBProtocolParser.CreateCommandFrame(FFBProtocolParser.FFBCommand.CMD_GET_DEVICE_INFO);
                    _serial.SendData(getDeviceInfo);//发送数据
                    
                }
                else
                {
                    MessageBox.Show("连接失败");
                }
            });
            DisConnect = new RelayCommand(()=>
            {
                _serial.Disconnect();
                IsConnected = false;
            });
            PortList = new ObservableCollection<string>();
            SetCenterPoint = new RelayCommand(()=>
            {
                StreeingAngle = 0;

                _serial.SendData(FFBProtocolParser.CreateCommandFrame(FFBProtocolParser.FFBCommand.CMD_SET_WHEEL_CENTER));
            });
            SetAngle = new RelayCommand<string?>((string? angle)=> {
                MaxStreeingAngle = int.Parse(angle??"360");
            });
            // 订阅串口数据接收事件
            _serial!.DataReceived += (sender, ms) =>
            {
                _dataParser?.ProcessStreamData(ms);
                ms.SetLength(0); // 清空内存流以便下次使用
            };

            _dataParser!.OnDeviceInfoReceived += (deviceInfo) =>
            {
                _deviceInfo.DeviceId = deviceInfo.DeviceId;
                _deviceInfo.FwVersion = deviceInfo.FirmwareVersionString;
                _deviceInfo.HardwareRev = deviceInfo.HardwareRev.ToString();
                _deviceInfo.MaxForce = deviceInfo.MaxForce;
                _deviceInfo.MaxAxis = deviceInfo.AxisCount;
                _deviceInfo.SerialNumber = deviceInfo.SerialNumber.ToString("X8");
                IsConnected = true;
            };
            OnRefreshPort();
        }

        private void OnRefreshPort()
        {
            var portSet = new HashSet<string>(Serial.GetAllSerialPortName());
            PortList = new ObservableCollection<string>(portSet);
        }
    }
}
